<template>
  <div class="box">

    <div class="boxon">
      <el-form :model="ruleForm" status-icon ref="ruleForm" class="demo-ruleForm">
        <el-form-item label="" prop="adminname" :rules="[
          { required: true, message: '请填写用户名' },
          { max: 6, message: '不能超过6位' }
        ]">
          <el-input type="text" v-model="ruleForm.adminname" placeholder="默认账号:admin" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="" prop="password" :rules="[
          { required: true, message: '密码不能为空' },
          { pattern: /^\d{6,}$/, message: '至少6位数字' }
        ]">
          <el-input type="password" v-model="ruleForm.password" placeholder="默认密码:123456" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { debounce, throttle } from 'lodash';
import { Message } from 'element-ui';
import filterRoutes from '@/utils/filterRouters';
import {asyncRoutes} from '@/router'
export default {
  data() {

    return {
   
      ruleForm: {
        adminname: '',
        password: '',
      },
    };
  },
  methods: {
    submitForm: debounce(function () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.$store.dispatch('login/login', this.ruleForm).then(res => {
            console.log(res,);
            if (res.code == 200) {
              let fil = filterRoutes(asyncRoutes, res.data.checkedkeys)
              fil.forEach(item => {
                this.$router.addRoute(item)
              })

              let path = this.$route.query.redirect || '/'
              this.$router.push(path)
              Message({
                type: 'success',
                message: '登录成功'
              })
            }
          })

        } else {
          Message({
            type: 'error',
            message: '账户名或密码错误'
          })
          console.log('error submit!!');
          return false;
        }
      });
    }, 1000),
  },

}
</script>

<style lang="scss" scoped>
.box {
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url('https://img0.baidu.com/it/u=1618563077,500052402&fm=253&fmt=auto&app=120&f=JPEG?w=1080&h=720') no-repeat;
  background-size: 100%;

  .boxon {
    width: 400px;
    height: 260px;
    background: #fff;
    box-sizing: border-box;
    border-radius: 10px;
    padding: 40px;

    button {
      width: 100%;
    }
  }
}
</style>